<?php

/**
 * This is the model class for table "tbtt_comment".
 *
 * The followings are the available columns in table 'tbtt_comment':
 * @property integer $comment_id
 * @property string $comment_author
 * @property string $comment_title
 * @property string $comment_content
 * @property string $comment_created_at
 * @property string $comment_updated_at
 * @property integer $commnent_status
 * @property string $comment_email
 * @property integer $news_id
 */
class Comment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Comment the static model class
	 */
        const STATUS_PENDING=1;
	const STATUS_APPROVED=2;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbtt_comment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('comment_author, comment_title, comment_content, comment_email', 'required'),
			array('comment_author, comment_email', 'length', 'max'=>128),
			array('comment_title', 'length', 'max'=>360),
                       
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('comment_id, comment_author, comment_title, comment_content, comment_created_at, comment_updated_at, commnent_status, comment_email, news_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'news' => array(self::BELONGS_TO, 'News', 'news_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'comment_id' => 'Comment',
			'comment_author' => 'Tên Người Viết',
			'comment_title' => 'Tiêu đề',
			'comment_content' => 'Nội dung',
			'comment_created_at' => 'Comment Created At',
			'comment_updated_at' => 'Comment Updated At',
			'commnent_status' => 'Commnent Status',
			'comment_email' => 'Email',
			'news_id' => 'News',
		);
	}
        public function approve()
	{
		$this->commnent_status=Comment::STATUS_APPROVED;
		$this->update(array('status'));
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
        protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord) {
				$this->comment_created_at = strval(date('Y-m-d H:i:s'));
				$this->comment_updated_at = strval(date('Y-m-d H:i:s'));
			}
			return true;
		}
		else
			return false;
	}
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('comment_id',$this->comment_id);
		$criteria->compare('comment_author',$this->comment_author,true);
		$criteria->compare('comment_title',$this->comment_title,true);
		$criteria->compare('comment_content',$this->comment_content,true);
		$criteria->compare('comment_created_at',$this->comment_created_at,true);
		$criteria->compare('comment_updated_at',$this->comment_updated_at,true);
		$criteria->compare('commnent_status',$this->commnent_status);
		$criteria->compare('comment_email',$this->comment_email,true);
		$criteria->compare('news_id',$this->news_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}